package com.fliangyun.nsa.nsamanage.utils;

import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.tea.TeaException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author fuliangyun@139.com
 * @date 2023年06月27日
 * @description
 */
@Slf4j
public class SmsUtil {
    /**
     * accessKeyId
     */
    private static final String ACCESS_KEY_ID = "";
    /**
     * accessKeySecret
     */
    private static final String ACCESS_KEY_SECRET = "";
    /**
     * 短信签名
     */
    private static final String SIGN_NAME = "";
    /**
     * 短信模板ID
     */
    private static final String TEMPLATE_CODE = "";

    /**
     * 使用AK&SK初始化账号Client
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String keyId, String keySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(keyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(keySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * 发送短信
     * @author fuliangyun@139.com
     * @date 2023-06-27 11:16
     * @param mobile 手机号码
     * @param msg 消息
     * @return void
     */
//    public static SendSmsResponseBody sendSms(String mobile, String msg, String templateCode){
//        try {
//            return sendSms(mobile, msg, templateCode);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    /**
     * 发送短信
     * @author fuliangyun@139.com
     * @date 2023-06-27 11:16
     * @param mobile 手机号码
     * @param msg 消息
     * @param templateCode 模板
     * @return void
     */
    public static SendSmsResponseBody sendSms(String mobile, String msg, String keyId, String keySecret, String sign, String templateCode) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = createClient(keyId, keySecret);
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setPhoneNumbers(mobile)
                .setSignName(sign)
                .setTemplateCode(templateCode)
                .setTemplateParam(msg);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, runtime);
            log.info("短信发送成功，Mobile：{}, Msg：{}, Body：{}", mobile, msg, JSON.toJSONString(response.getBody()));
            return response.getBody();
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return null;
    }

}
